/**
 * 
 */
package hu.gbalage.owl.cms.disec.flickr.auth;

import hu.gbalage.owl.cms.disec.flickr.FlickrService;

import java.io.IOException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.auth.AuthInterface;
import com.aetrion.flickr.auth.Permission;

/**
 * @author balage
 *
 */
public class FlickrAuth extends HttpServlet {
	
	private static final long serialVersionUID = 7731175488666276224L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	
		Flickr f;
		try {
			f = FlickrService.createFlickr();
		} catch (ParserConfigurationException e) {
			throw new ServletException(e);
		}
	    
		AuthInterface authInterface = f.getAuthInterface();
		URL url;
		try {
			url = authInterface.buildAuthenticationUrl(Permission.READ, authInterface.getFrob());
		} catch (SAXException e) {
			throw new ServletException(e);
		} catch (FlickrException e) {
			throw new ServletException(e);
		}
		
		resp.sendRedirect(url.toString());
	}
	
}
